package com.jiashn.minio.config;

import com.alibaba.druid.filter.Filter;
import com.alibaba.druid.filter.logging.Slf4jLogFilter;
import com.alibaba.druid.filter.stat.StatFilter;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ResourceLoader;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.util.*;

/**
 * @author: jiangjs
 * @description: druid连接池配置
 * @date: 2023/10/20 14:55
 **/
@Configuration
public class DruidDataSourceConfig {

    @Resource
    private ResourceLoader resourceLoader;

    @Bean
    @ConfigurationProperties(prefix = "spring.datasource.druid")
    public DataSource baseDataSource(){
        DruidDataSource build = DruidDataSourceBuilder.create().build();
        List<Filter> filters = new ArrayList<>();
        filters.add(statFilter());
        filters.add(logFilter());
        build.setProxyFilters(filters);
        build.setPasswordCallback(new RsaDBPasswordCallBack(resourceLoader));
        return build;

    }

    @Bean
    @ConditionalOnClass(DruidDataSource.class)
    public ServletRegistrationBean<StatViewServlet> setStatViewServlet(){
        ServletRegistrationBean<StatViewServlet> registrationBean = new ServletRegistrationBean<>(new StatViewServlet(), "/druid/*");
        Map<String, String> params = new HashMap<>(4);
        params.put("loginUsername","admin");
        params.put("loginPassword","admin");
        params.put("allow","127.0.0.1");
        params.put("deny","");
        registrationBean.setInitParameters(params);
        return registrationBean;
    }

    @Bean
    @ConditionalOnClass(DruidDataSource.class)
    public FilterRegistrationBean<javax.servlet.Filter> webStatFilter(){
        FilterRegistrationBean<javax.servlet.Filter> bean = new FilterRegistrationBean<>();
        bean.setFilter(new WebStatFilter());
        Map<String, String> params = new HashMap<>(1);
        params.put("exclusions","*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
        bean.setInitParameters(params);
        bean.setUrlPatterns(Collections.singletonList("/*"));
        return bean;
    }

    @Bean(name = "statFilter")
    public StatFilter statFilter(){
        StatFilter statFilter = new StatFilter();
        statFilter.setSlowSqlMillis(30L);
        statFilter.setLogSlowSql(true);
        statFilter.setMergeSql(true);
        return statFilter;
    }

    @Bean(name = "logFilter")
    public Slf4jLogFilter logFilter(){
        Slf4jLogFilter logFilter = new Slf4jLogFilter();
        logFilter.setDataSourceLogEnabled(true);
        logFilter.setStatementExecutableSqlLogEnable(true);
        return logFilter;
    }


}
